/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;

/**
 *
 * @author Kvest
 */
public class ChernovtsiTimeSelectForm extends TimeSelectForm implements ActionListener
{
    private static final int SELF_TIME_ITEM_ID = 255;
    public static final int SELF_TIME_OK_ID = 3;
    public static final int SELF_TIME_BACK_ID = 4;
    
    private Command ok;
    private Command back;
    private FormAction listener;

    private ThumbList time;
    private String selectedTime;

    private Command selfTimeOk;
    private Command selfTimeBack;
    
    private TextField selfTime;
    
    public ChernovtsiTimeSelectForm(String title, String backCommandName, FormAction listener)
    {
        super(title);
        this.listener = listener;

        init(backCommandName);
    }
    
    private void init(String backCommandName)
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addCommandListener(this);

        back = new Command(backCommandName, BACK_ID);
        ok = new Command("Ok", OK_ID);

	time = new ThumbList();
        time.setSelectCommandText("");
        time.setFireOnClick(false);
        time.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        render.getStyle().setPadding(Component.BOTTOM, 0);
        render.getStyle().setPadding(Component.TOP, 0);
        time.setRenderer(render);
        time.getStyle().setPadding(Component.BOTTOM, 0);
        time.getStyle().setPadding(Component.TOP, 0);
        time.setSelectCommandText("");
 //       time.addActionListener(this);

        time.addItem(new CellItem(0, "2"));
        time.addItem(new CellItem(1, "5"));
        time.addItem(new CellItem(2, "7"));
        time.addItem(new CellItem(3, "10"));
        time.addItem(new CellItem(4, "12"));
        time.addItem(new CellItem(SELF_TIME_ITEM_ID, "Cвое время"));
        
        //Компоненты для выбора своего времени
        selfTimeOk = new Command("Ok", SELF_TIME_OK_ID);
        selfTimeBack = new Command(backCommandName, SELF_TIME_BACK_ID);
        
        selfTime = new TextField();
        selfTime.setSelectCommandText("");
        selfTime.setInputModeOrder(new String[]{"123"});
        selfTime.setConstraint(TextArea.NUMERIC);
    }
    
    public String getTimeStr()
    {
        return selectedTime;
    }
    
    public void reset()
    {
        time.setSelectedIndex(1);
        selectedTime = "5";
        selfTime.setText(selectedTime);
        
        showTimesList();
    }
    
    private void clear()
    {
        removeAllCommands();
        
        removeComponent(selfTime);
        removeComponent(time);
    }
    
    private void showTimesList()
    {
        clear();
        
        addCommand(back);
        addCommand(ok);

        addComponent(time);
        setFocused(time);
    }
    
    private void showSelfTimeField()
    {
        clear();
        
        addCommand(selfTimeBack);
        addCommand(selfTimeOk);

        addComponent(selfTime);
        setFocused(selfTime);
    }
    
    public void actionPerformed(ActionEvent evt)
    {
        int selectedCommandId = evt.getCommand().getId();
        
        if (selectedCommandId == OK_ID)
        {
            int selectedItemId= ((CellItem)time.getSelectedItem()).getId();
            if (selectedItemId == SELF_TIME_ITEM_ID)
            {
                showSelfTimeField();
                return;
            }
            else
            {
                switch(selectedItemId)
                {
                    case 0: selectedTime = "2"; break;
                    case 1: selectedTime = "5"; break;
                    case 2: selectedTime = "7"; break;
                    case 3: selectedTime = "10"; break;
                    case 4: selectedTime = "12"; break;
                }
            }
        }
        else if (selectedCommandId == SELF_TIME_BACK_ID)
        {
            showTimesList();
            return;
        }
        else if (selectedCommandId == SELF_TIME_OK_ID)
        {
            selectedTime = selfTime.getText();
            if (selectedTime.equals(""))
            {
                selectedTime = "5";
            }
            selectedCommandId = OK_ID;
        }
            
        if (listener != null)
        {
            listener.commandAction(selectedCommandId);
        }
    }
}
